
package harlequin;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class ShowEdit extends  AddEditDeleteShow{
    

    private String editquery = "UPDATE play SET title=?, dateTimeslot = ?, price=?, slot=? where dateTimeslot = ?";

    
    public ShowEdit(Connection connector) {
        this.connector = connector;
        this.setLocationByPlatform(rootPaneCheckingEnabled);
        this.setTitle("Edit Show");
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tTitle = new javax.swing.JTextField();
        tPrice = new javax.swing.JTextField();
        tSlot = new javax.swing.JTextField();
        bEditShow = new javax.swing.JButton();
        bBack = new javax.swing.JButton();
        tDateTimeslot = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel2.setText("Date-Timeslot:");

        jLabel1.setText("Title:");

        jLabel3.setText("Price:");

        jLabel4.setText("Slot:");

        tTitle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tTitleActionPerformed(evt);
            }
        });

        bEditShow.setText("Edit");
        bEditShow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bEditShowActionPerformed(evt);
            }
        });

        bBack.setText("Back");
        bBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jLabel1)
                .addGap(60, 60, 60)
                .addComponent(tTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jLabel2)
                .addGap(5, 5, 5)
                .addComponent(tDateTimeslot, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jLabel3)
                .addGap(57, 57, 57)
                .addComponent(tPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jLabel4)
                .addGap(63, 63, 63)
                .addComponent(tSlot, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bEditShow, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bBack, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel1))
                    .addComponent(tTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel2))
                    .addComponent(tDateTimeslot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel3))
                    .addComponent(tPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel4))
                    .addComponent(tSlot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(128, 128, 128)
                .addComponent(bEditShow)
                .addGap(7, 7, 7)
                .addComponent(bBack)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

 
private void bEditShowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bEditShowActionPerformed
   dateTimeslot = tDateTimeslot.getText();  
   String tempDate = dateTimeslot;
    try {
            
            
            title = tTitle.getText();
            dateTimeslot = tDateTimeslot.getText();
            slot = tSlot.getText();
            price = tPrice.getText();           
            
            if(title.isEmpty() && dateTimeslot.isEmpty() && price.isEmpty() && slot.isEmpty() )
             JOptionPane.showMessageDialog(null, "Update failed! \nPlease input all empty fields.");
            else if(title.isEmpty())
             JOptionPane.showMessageDialog(null, "Update failed! \nPlease input the Title");
            else if(dateTimeslot.isEmpty())
             JOptionPane.showMessageDialog(null, "Update failed!\n Please input the Date");
            else if (price.isEmpty())
                JOptionPane.showMessageDialog(null, "Update failed!\n Please input the Price");
            else if(slot.isEmpty())
             JOptionPane.showMessageDialog(null, "Update failed!\n Please input number Slots");
            //else if (DateExists(dateTimeslot))
              //  JOptionPane.showMessageDialog(null, "Update failed!\n Date already exsists");
            else if (dateTimeslot.length() < 18)
                JOptionPane.showMessageDialog(null, "Update failed!\n Date format should be: mm/dd/yy");
            else
            
            {
            java.sql.PreparedStatement preparedStatement = connector.prepareStatement(editquery);
            preparedStatement.setString(1, title);
            preparedStatement.setString(2, dateTimeslot);
            preparedStatement.setString(3, price);
            preparedStatement.setString(4, slot);
            preparedStatement.setString(5, tempDate);
            
            preparedStatement.executeUpdate();
            
            JOptionPane.showMessageDialog(null, "Edit Success!");
               this.dispose(); 

            }
        } catch (SQLException ex) {
            Logger.getLogger(ShowEdit.class.getName()).log(Level.SEVERE, null, ex);
        }

}//GEN-LAST:event_bEditShowActionPerformed

private void bBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBackActionPerformed
    

    this.dispose();
}//GEN-LAST:event_bBackActionPerformed

private void tTitleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tTitleActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_tTitleActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bBack;
    private javax.swing.JButton bEditShow;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField tDateTimeslot;
    private javax.swing.JTextField tPrice;
    private javax.swing.JTextField tSlot;
    private javax.swing.JTextField tTitle;
    // End of variables declaration//GEN-END:variables
}
